﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;

public class ClassroomController : MonoBehaviour {
	public UIButton[] objects;
	public UIButton[] colors;
	public UILabel lblQuestion;
	private int questSelectedIndex = 0;
	private bool isColorSelectionOk = false;
	private List<Quest> listQuestion;
	private string [] listTextQuest = {"Paint the board white","Paint the desk blue","Paint the book red","Paint the bag yellow","Paint the computer black","Paint the scissors silver"};
	private string BASE_AUDIO_PATH = "Sounds/Classroom/";
	private string [] listSound = {"sound_1","sound_2","sound_3","sound_4","sound_5","sound_6"};
	private bool isWaitForNextQuest = false;
	public GameObject boy;
	// Use this for initialization
	void Start () {
		initQuest ();
		setButtonClick ();
		StartCoroutine (nextQuestion ());
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void setButtonClick () {
		for(int i = 0;i<colors.Length;i++) {
			UIButton btn = colors[i];
			//btn.name = i.ToString();
			btn.onClick.Clear();
			btn.onClick.Add (new EventDelegate(this,"onColorClick"));
		}

		for(int i = 0;i<objects.Length;i++) {
			UIButton btn = objects[i];
			btn.onClick.Clear();
			btn.onClick.Add (new EventDelegate(this,"onObjectClick"));
		}

	}

	private void onColorClick () {
		UIButton current = UIButton.current;

		if (current.GetComponent <ButtonItemControler> ().index != listQuestion[questSelectedIndex-1].answerIndex) {//check if incorrect selection color
			playSoundFalseAnswer();
			isColorSelectionOk = false;
		}else {
			isColorSelectionOk = true;
			playSoundTrueAnswer ();
		}

		resetScaleColorButton ();
		Vector3 scale = current.transform.localScale;
		scale.x = 1.3f;
		scale.y = 1.3f;
		current.transform.localScale = scale;
		current.GetComponent<Animator>().SetBool("isSelected", true );
		current.GetComponent<Animator>().SetBool("isNormal", false );
	}

	private void onObjectClick () {
		UIButton current = UIButton.current;

		if (current.GetComponent <ButtonItemControler> ().index!= listQuestion[questSelectedIndex-1].answerIndex || !isColorSelectionOk) {//check if incorrect selection color
			playSoundFalseAnswer();
			return;
		}

		string currentSpriteName = current.GetComponent<UISprite> ().spriteName;
		current.normalSprite = currentSpriteName + "_color";
		current.enabled = false;
		isColorSelectionOk = false;
		playSoundTrueAnswer ();
		StartCoroutine (nextQuestion ());
	}

	public void onResetClick () {
		Application.LoadLevel (Application.loadedLevelName);
	}
	private void resetScaleColorButton () {
		foreach (UIButton btn in colors) {
			if(btn.transform.localScale.x >1) {;
				Vector3 scale = new Vector3(1.0f,1.0f,1.0f);
				btn.transform.localScale = scale;
				btn.GetComponent<Animator>().SetBool("isSelected", false );
				btn.GetComponent<Animator>().SetBool("isNormal", true );
			}
		}
	}


	public void onSpeakClick () {
		if (isWaitForNextQuest) {
			return ;
		}
		AudioClip clip = Resources.Load (BASE_AUDIO_PATH + listQuestion [questSelectedIndex - 1].audioPath) as AudioClip;
		AudioSource.PlayClipAtPoint (clip, transform.position);
	}

	private IEnumerator nextQuestion () {
		isWaitForNextQuest = true;
		yield return new WaitForSeconds (1f);
		if (questSelectedIndex >= listQuestion.Count) {
			endGame ();
		} else {
			lblQuestion.text = listQuestion [questSelectedIndex].textQuestion;
			AudioClip clip = Resources.Load (BASE_AUDIO_PATH + listQuestion [questSelectedIndex].audioPath) as AudioClip;
			AudioSource.PlayClipAtPoint (clip, transform.position);
			questSelectedIndex += 1;
			isWaitForNextQuest = false;
		}
	}

	private void initQuest () {
		listQuestion= new List<Quest> ();
		for (int i=0;i<listTextQuest.Length;i++) {
			Quest q = new Quest (listTextQuest[i],listSound[i],i);
			listQuestion.Add(q);
		}
		ListUtils.shuffle (listQuestion);
	}

	private void playSoundTrueAnswer () {
		AudioClip clip = Resources.Load (Constants.AUDIO_CORRECT_ANSWER_PATH) as AudioClip;
		AudioSource.PlayClipAtPoint(clip, transform.position);
	}

	private void playSoundFalseAnswer () {
		AudioClip clip = Resources.Load (Constants.AUDIO_INCORRECT_ANSWER_PATH) as AudioClip;
		AudioSource.PlayClipAtPoint(clip, transform.position);
	}
	private void playSoundGameComplete () {
		AudioClip clip1 = Resources.Load ("Sounds/Common/well_done") as AudioClip;
		AudioSource.PlayClipAtPoint(clip1, transform.position);

		AudioClip clip2 = Resources.Load ("Sounds/Common/sound_game_coplete") as AudioClip;
		AudioSource.PlayClipAtPoint(clip2, transform.position);
	}

	private void endGame () {
		playSoundGameComplete ();
		lblQuestion.text = "Well done!!";
		boy.GetComponent<TweenPosition> ().enabled = true;
		foreach(UIButton b  in colors) {
			b.enabled  =false;
		}
		foreach(UIButton o  in objects) {
			Animator anim = o.GetComponent<Animator>();
			if(anim!=null) {
				anim.enabled =true;
			}
		}

	}
}
